class BlogsController < ApplicationController
  def index
    list
    render :action => 'list'
  end
  
  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
  :redirect_to => { :action => :list }
  
  
  
  
  def list
    if params[:new_filter] && params[:new_filter] == "true"
    reset_session
    end
    setup_session
    @authorities=Authority.find(:all)
    if session[:authority] && session[:authority] != ""
    @authority=Authority.find(session[:authority]) 
    end
    @feeds=Feed.filtered_feeds(session[:tags], session[:authority], session[:combinetype], session[:sortby], session[:searchtext])
    @feeds_pages, @feeds = paginate_collection(@feeds, {:per_page => session[:per_page], :page => session[:page]})
  end
  
  def show
    @feed = Feed.find(params[:id])
  end
  
  def new
    @feed = Feed.new
  end
  
  def create
    @feed = Feed.new(params[:feed])
    if @feed.save
      @feed.tag_with(params[:taglist])
      @feed.save
      flash[:notice] = 'Feed was successfully created.'
      redirect_to :action => 'list', :authority=>@feed.authority_id
    else
      render :action => 'new'
    end
  end
  
  def edit
    @feed = Feed.find(params[:id])
  end
  
  def update
    @feed = Feed.find(params[:id])
    if @feed.update_attributes(params[:feed])
      flash[:notice] = 'Feed was successfully updated.'
      redirect_to :action => 'show', :id => @feed
    else
      render :action => 'edit'
    end
  end
  
  def list_items
    list_setup
    render :partial => "list_feeds"
  end
  
  def list_items_for_one_feed
    @feed=Feed.find(params[:id])
    render :partial=>"list_items_for_one_feed"
  end
  
  def hide_items
    @feed=Feed.find(params[:id])
    render :partial=>"hide_items"
  end
  
  def destroy
    Feed.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
  
  def tag_cloud_display
    render :partial=>"tag_cloud_display"
  end
  
  def tag
    @feed = Feed.find(params[:id])
    @feed.tag_with(params[:tag_list])
    @feed.save
    #render :partial => "content", 
    #        :locals => {:feed => feed, :form_id => params[:form_id]}
  end
  
  def opml
    @feeds=Feed.filtered_feeds(session[:tags], session[:authority], session[:combinetype], session[:sortby], session[:searchtext])
    headers["Content-Type"]="text/xml"
    render :partial=>'shared/opml'
  end
  
  def rss
    @description="A feed of posts "
    if params[:authority] && params[:authority!]!="" && authority=Authority.find(params[:authority]) rescue nil
      @description+=" from #{authority.name} blogs"
    else
      @description+=" all blogs in the ScotEdublogs site"
    end
    if params[:tags] && params[:tags]!=[]
      @description+=" tagged with #{params[:combinetype] || "any"} of "
      @description+= params[:tags].join(", ")
    end
    @description+="."
    @items=Feed.items_from_filtered_feeds(params[:tags], params[:authority], params[:combinetype], session[:sortby], session[:searchtext])
    render :partial=>'shared/rss'
  end
  
  def ajax_list_items
   list_setup
  end
  
  private
  
  def list_setup #this method is used by list_items and ajax_list_items
    setup_session
    @authorities=Authority.find(:all)
    if session[:authority] && session[:authority] != ""
      @authority=Authority.find(session[:authority]) 
    end
    @feeds=Feed.filtered_feeds(session[:tags], session[:authority], session[:combinetype], session[:sortby], session[:searchtext])
    @feeds_pages, @feeds = paginate_collection(@feeds, {:per_page => session[:per_page], :page => session[:page]})
  end
  
  def setup_session
 
    # search string
    if params[:searchtext] && params[:searchtext]!=""
      session[:searchtext] = params[:searchtext]
    end

 
    # store authority information
    if params[:authority] && params[:authority]!=""
      session[:authority] = params[:authority]
    end
    
    if params[:authority] && params[:authority]=="none"
      session[:authority] = ""
    end


  # sort fun
    if params[:sortby] && params[:sortby]!=""
      session[:sortby] = params[:sortby]
    end
    
    if params[:sortby] && params[:sortby]=="none"
      session[:sortby] = "atoz"
    end
      
     
    # check for per page change
    if params[:per_page] && params[:per_page].to_i != 0
      session[:per_page] = params[:per_page].to_i
    elsif !session[:per_page]
      # otherwise set default
      session[:per_page] = 10
    end
    
    #same for page number
    if params[:page] && params[:page].to_i != 0
      session[:page] = params[:page].to_i
    else
      # otherwise set default
      session[:page] = 1
    end
    
    #clear tag selection
    if params[:cleartags] && params[:cleartags] == "true"
      session[:tags] = []
    end
    
    # deal with param for combining tags
    if params[:combinetype] && (params[:combinetype]=="any" || params[:combinetype]=="all")
      session[:combinetype] = params[:combinetype]
    else 
      session[:combinetype] = "any" if !session[:combinetype] 
    end
    
    #deal with stored taglist
    session[:tags] = [] if !session[:tags]
    # pick up new tag id and use to remove or add tag to list
    if params[:tag_name] && params[:tag_name] != ""
      # check list
      if session[:tags].include?(params[:tag_name])
        # already there so remove
        session[:tags] = session[:tags] - [params[:tag_name]]
      else
        session[:tags] = session[:tags] + [params[:tag_name]]
      end
    end
  end
  
end
